<%@ WebHandler Language="C#" Class="IFoto" %>

using System;
using System.Web;
using System.IO;

public class IFoto : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        if (context.Request["IdFoto"] != null)
        {
            MostrarFoto(context, context.Request["IdFoto"], context.Request["max"]);
        }
        
    }

    private void MostrarFoto(HttpContext context, String IdFoto, string notumbnail)
    {
        FotoDato fotobean = DBMonitoreo.findMonitoreoFotoBean(IdFoto);
        if (fotobean != null)
        {

            byte[] b1 = fotobean.Foto;
            if (notumbnail == null)
            {
                MemoryStream ms = new MemoryStream(b1);
                System.Drawing.Image loDrawImage = System.Drawing.Image.FromStream(ms);

                System.Drawing.Image imgThumb = loDrawImage.GetThumbnailImage(50, 50, null, new IntPtr());
                // make a memory stream to work with the image bytes
                MemoryStream imageStream = new MemoryStream();
                imgThumb.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] bmpBytes = imageStream.GetBuffer();
                imgThumb.Dispose();
                loDrawImage.Dispose();
                ms.Close();
                imageStream.Close();
                b1 = bmpBytes;
            }

            context.Response.ContentType = "image/jpeg";

            /* SI SE DESCOMENTA LAS LINEAS DE ARRIBA, LA SIGUIENTE DEBE SER COMENTADA */
            context.Response.BinaryWrite(b1);
        }
    }

    private void subDescargaFoto(HttpContext context, String IdFoto, string notumbnail)
    {
        FotoDato fotobean = DBMonitoreo.findMonitoreoFotoBean(IdFoto);
        if (fotobean != null)
        {

            byte[] b1 = fotobean.Foto;
            if (notumbnail == null)
            {
                MemoryStream ms = new MemoryStream(b1);
                System.Drawing.Image loDrawImage = System.Drawing.Image.FromStream(ms);

                System.Drawing.Image imgThumb = loDrawImage.GetThumbnailImage(50, 50, null, new IntPtr());
                // make a memory stream to work with the image bytes
                MemoryStream imageStream = new MemoryStream();
                imgThumb.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] bmpBytes = imageStream.GetBuffer();
                imgThumb.Dispose();
                loDrawImage.Dispose();
                ms.Close();
                imageStream.Close();
                b1 = bmpBytes;
            }

            context.Response.ContentType = "image/jpeg";

            /* SI SE DESCOMENTA LAS LINEAS DE ARRIBA, LA SIGUIENTE DEBE SER COMENTADA */
            context.Response.BinaryWrite(b1);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}